/*
 * LoopThread.h
 *
 *  Created on: 2020年9月17日
 *      Author: chuanjiang.zh
 */

#ifndef UTIL_LOOPTHREAD_H_
#define UTIL_LOOPTHREAD_H_

#include "BasicType.h"
#include "TThread.h"
#include <functional>
#include <deque>
#include <mutex>
#include "TEvent.h"


class LoopThread : public comn::Thread
{
public:
	typedef std::function< void() >		Task;
	typedef std::deque< Task >			TaskQueue;

	typedef std::function< void(int) >	TimerHandler;

	enum Constant
	{
		IDLE_DURATION = 1000 * 10,	/// millisecond
	};

public:
	LoopThread();
	virtual ~LoopThread();

	void post(Task task);
	void clearTask();


protected:
	virtual void onIdle();

protected:
	virtual int run();
	virtual void doStop();


protected:
	bool fetchAndHandle();
	bool fetchTask(Task& task);

protected:

	std::mutex		m_mutex;
	comn::Event		m_eventTask;
	TaskQueue		m_taskQueue;


};

#endif /* UTIL_LOOPTHREAD_H_ */
